package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysBrowsingHistory;
import com.ruoyi.system.domain.SysCollection;
import com.ruoyi.system.domain.SysPosts;
import com.ruoyi.system.domain.SysUsers;
import com.ruoyi.system.mapper.SysUsersMapper;
import com.ruoyi.system.service.ISysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-01
 */
@Service
public class SysUsersServiceImpl implements ISysUsersService {
    @Autowired
    private SysUsersMapper sysUsersMapper;

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public SysUsers selectSysUsersByUserId(Long userId) {
        return sysUsersMapper.selectSysUsersByUserId(userId);
    }

    /**
     * 查询用户列表
     *
     * @param sysUsers 用户
     * @return 用户
     */
    @Override
    public List<SysUsers> selectSysUsersList(SysUsers sysUsers) {
        return sysUsersMapper.selectSysUsersList(sysUsers);
    }

    /**
     * 新增用户
     *
     * @param sysUsers 用户
     * @return 结果
     */
    @Override
    public int insertSysUsers(SysUsers sysUsers) {
        return sysUsersMapper.insertSysUsers(sysUsers);
    }

    /**
     * 修改用户
     *
     * @param sysUsers 用户
     * @return 结果
     */
    @Override
    public int updateSysUsers(SysUsers sysUsers) {
        return sysUsersMapper.updateSysUsers(sysUsers);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUsersByUserIds(Long[] userIds) {
        return sysUsersMapper.deleteSysUsersByUserIds(userIds);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUsersByUserId(Long userId) {
        return sysUsersMapper.deleteSysUsersByUserId(userId);
    }

    /**
     * 新增发布信息
     *
     * @param sysUsers 用户对象
     */
    public void insertSysPosts(SysUsers sysUsers) {
        List<SysPosts> sysPostsList = sysUsers.getSysPostsList();
        Long userId = sysUsers.getUserId();
        if (StringUtils.isNotNull(sysPostsList)) {
            List<SysPosts> list = new ArrayList<SysPosts>();
            for (SysPosts sysPosts : sysPostsList) {
                sysPosts.setUserId(userId);
                list.add(sysPosts);
            }
            if (list.size() > 0) {
                sysUsersMapper.batchSysPosts(list);
            }
        }
    }
    /**
     * 新增收藏信息
     *
     * @param sysUsers 用户对象
     */
    public void insertSysCollection(SysUsers sysUsers)
    {
        List<SysCollection> sysCollectionList = sysUsers.getSysCollectionList();
        Long userId = sysUsers.getUserId();
        if (StringUtils.isNotNull(sysCollectionList))
        {
            List<SysCollection> list = new ArrayList<SysCollection>();
            for (SysCollection sysCollection : sysCollectionList)
            {
                sysCollection.setUserId(userId);
                list.add(sysCollection);
            }
            if (list.size() > 0)
            {
                sysUsersMapper.batchSysCollection(list);
            }
        }
    }
    /**
     * 新增浏览历史信息
     *
     * @param sysUsers 用户对象
     */
    public void insertSysBrowsingHistory(SysUsers sysUsers)
    {
        List<SysBrowsingHistory> sysBrowsingHistoryList = sysUsers.getSysBrowsingHistoryList();
        Long userId = sysUsers.getUserId();
        if (StringUtils.isNotNull(sysBrowsingHistoryList))
        {
            List<SysBrowsingHistory> list = new ArrayList<SysBrowsingHistory>();
            for (SysBrowsingHistory sysBrowsingHistory : sysBrowsingHistoryList)
            {
                sysBrowsingHistory.setUserId(userId);
                list.add(sysBrowsingHistory);
            }
            if (list.size() > 0)
            {
                sysUsersMapper.batchSysBrowsingHistory(list);
            }
        }
    }
}
